var addressController;
$(function () {
    addressController = new Vue({
        el:'#addressController',
        data:{
            nav:_mineNav,
            list:[],
            address:{
                id:'',
                userName:'',
                area:'',
                address:'',
                phone:''
            },
            showAddress:false
        },
        methods:{
            show:function (o) {
                this.showAddress = o;
                if (!o){
                    this.address = {
                            id:'',
                            userName:'',
                            area:'',
                            address:'',
                            phone:''
                    }
                }
            },
            addAddress:function () {
                var that = this;
                var url = '/api/userInfo/addMyAddress';
                if (that.address.id){
                    url = '/api/userInfo/updateMyAddress';
                }
                var address = that.address;
                if (!address.userName){
                    sy.msg("请输入联系人");
                    return;
                }
                if (!address.phone){
                    sy.msg("请输入手机号");
                    return;
                }
                if (!sy.isPhone(address.phone)){
                    sy.msg("请输入正确手机号");
                    return;
                }
                if (!address.area){
                    sy.msg("请输入所在省市区");
                    return;
                }
                if (!address.address){
                    sy.msg("请输入详细地址");
                    return;
                }

                sy.postApi(url,that.address,function () {
                    that.show(false);
                    that.loadData();
                })
            },
            editAddress:function (item) {
                this.address = item;
                this.address.userName = item.name;
                this.address.type = 1;
                this.show(true);
            },
            delAddress:function (item) {
                var that = this;
                sy.postApi('/api/userInfo/updateMyAddress',{
                    id:item.id,
                    type:2
                },function (data) {
                    that.loadData();
                })
            },
            setDefAddress:function (item) {
                var that = this;
                sy.postApi('/api/userInfo/setDefAddress',{
                    id:item.id
                },function (data) {
                    that.loadData();
                })
            },
            loadData:function () {
                var that = this;
                sy.postApi('/api/userInfo/myAddressList',{},function (data) {
                    that.list = data;
                })
            }
        },
        created:function () {
            this.loadData();
        },
        mounted:function () {
            var that = this;
            // 选择省市区
            $("#area").click(function (e) {
                SelCity(this,e,function (area){
                    that.address.area = area;
                });
                $(document).find("#PoPy").css({
                    position:'fixed',
                    top: '532px',
                    left: '50%',
                    marginLeft:'-279px'
                });
            });
        }
    });
})